package com.edu.service.impl;

import com.edu.entity.PhoneCategory;
import com.edu.entity.PhoneInfo;
import com.edu.entity.PhoneSpecs;
import com.edu.enums.ResultEnum;
import com.edu.exception.PhoneException;
import com.edu.repository.PhoneCategoryRepository;
import com.edu.repository.PhoneInfoRepository;
import com.edu.repository.PhoneSpecsRepository;
import com.edu.service.PhoneService;
import com.edu.util.PhoneUtil;
import com.edu.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class PhoneServiceImpl implements PhoneService {
    @Autowired
    private PhoneCategoryRepository categoryRepository;
    @Autowired
    private PhoneInfoRepository infoRepository;
    @Autowired
    private PhoneSpecsRepository specsRepository;

    @Override
    public DataVO findDataVO() {
        DataVO dataVO = new DataVO();

        List<PhoneCategory> categoryList = categoryRepository.findAll();
        List<PhoneCategoryVO> categories = categoryList.stream().map(e -> new PhoneCategoryVO(e.getCategoryName(), e.getCategoryType())).collect(Collectors.toList());
        dataVO.setCategories(categories);

        List<PhoneInfo> infoList = infoRepository.findByCategoryType(categoryList.get(0).getCategoryId());
        List<PhoneInfoVO> infos = infoList.stream().map(e -> new PhoneInfoVO(e.getPhoneId(),
                e.getPhoneName(), e.getPhonePrice().toString(),
                e.getPhoneDescription(), e.getPhoneIcon(),
                PhoneUtil.createTag(e.getPhoneTag()))).collect(Collectors.toList());
        dataVO.setPhones(infos);

        return dataVO;
    }

    @Override
    public List<PhoneInfoVO> findPhoneInfoVOByCategoryType(Integer categoryType) {

        List<PhoneInfo> infoList = infoRepository.findByCategoryType(categoryType);
        List<PhoneInfoVO> infos = infoList.stream().map(e -> new PhoneInfoVO(e.getPhoneId(),
                e.getPhoneName(), e.getPhonePrice() .toString(),
                e.getPhoneDescription(), e.getPhoneIcon(),
                PhoneUtil.createTag(e.getPhoneTag()))).collect(Collectors.toList());
        return infos;
    }

    @Override
    public SpecsPackageVO findSpecsByPhoneId(Integer phoneId) {
        PhoneInfo phoneInfo = infoRepository.findById(phoneId).get();
        List<PhoneSpecs> phoneSpecsList = specsRepository.findAllByPhoneId(phoneId);

        ArrayList<PhoneSpecsVO> phoneSpecsVOList = new ArrayList<>();
        ArrayList<PhoneSpecsCasVO> phoneSpecsCasVOList = new ArrayList<>();

        PhoneSpecsVO phoneSpecsVO;
        PhoneSpecsCasVO phoneSpecsCasVO;
        for (PhoneSpecs phoneSpecs : phoneSpecsList) {
            phoneSpecsVO = new PhoneSpecsVO();
            phoneSpecsCasVO = new PhoneSpecsCasVO();
            BeanUtils.copyProperties(phoneSpecs, phoneSpecsVO);
            BeanUtils.copyProperties(phoneSpecs, phoneSpecsCasVO);
            phoneSpecsVOList.add(phoneSpecsVO);
            phoneSpecsCasVOList.add(phoneSpecsCasVO);
        }

        TreeVO treeVO = new TreeVO();
        treeVO.setV(phoneSpecsVOList);

        List<TreeVO> treeVOList = new ArrayList<>();
        treeVOList.add(treeVO);

        Integer price = phoneInfo.getPhonePrice().intValue();


        SkuVO skuVO = new SkuVO();
        skuVO.setTree(treeVOList);
        skuVO.setList(phoneSpecsCasVOList);
        skuVO.setPrice(price.toString());
        skuVO.setStock_num(phoneInfo.getPhoneStock());

        Map<String,String> goods = new HashMap<>();
        goods.put("picture", phoneInfo.getPhoneIcon());
        SpecsPackageVO specsPackageVO = new SpecsPackageVO();
        specsPackageVO.setGoods(goods);
        specsPackageVO.setSku(skuVO);

        return specsPackageVO;
    }

    @Override
    public void subStock(Integer specsId, Integer quantity) {
        PhoneSpecs phoneSpecs= specsRepository.findById(specsId).get();
        PhoneInfo phoneInfo = infoRepository.findById(phoneSpecs.getPhoneId()).get();
        Integer resule = phoneSpecs.getSpecsStock()-quantity;
        if (resule < 0) {
            log.error("【库存】库存不足");
            throw new PhoneException(ResultEnum.PHONE_STOCK_ERROR);
        }

        phoneSpecs.setSpecsStock(resule);
        specsRepository.save(phoneSpecs);
        resule = phoneInfo.getPhoneStock() - quantity;
        if (resule < 0) {
            log.error("【库存】库存不足");
            throw new PhoneException(ResultEnum.PHONE_STOCK_ERROR);
        }
        phoneInfo.setPhoneStock(resule);
        infoRepository.save(phoneInfo);


    }
}
